package com.qingkai.onlineshop.presentation.detail

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.qingkai.onlineshop.app.Route
import com.qingkai.onlineshop.data.AppDatabase
import com.qingkai.onlineshop.presentation.dashboard.testItemEntity
import com.qingkai.onlineshop.presentation.dashboard.testItemFull
import com.qingkai.onlineshop.ui.theme.OnLineShopAppTheme
import org.koin.androidx.compose.koinViewModel
import org.koin.core.parameter.parametersOf

/**
 *作者：created by 89270 on 2025/6/13 18:08
 *描述：
 */
@Composable
private fun injectPreviewModel(param: Route.Detail): DetailViewModel {
    val context = LocalContext.current
    return remember {
        DetailViewModel(
            appDatabase = AppDatabase.getInstance(context),
            param = param
        )
    }
}

@Composable
fun DetailScreen(
    isPreview: Boolean = false,
    param: Route.Detail,
    upPress: () -> Unit = {},
    navigateToRoute: (Route) -> Unit = {}
) {
    val viewModel = if (isPreview) injectPreviewModel(param) else koinViewModel {
        parametersOf(param)
    }
    val state by viewModel.state.collectAsStateWithLifecycle()
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
    ) {
        if (isPreview || state.itemFull != null) {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(rememberScrollState())
            ) {
                ProductImages(
                    modifier = Modifier
                        .zIndex(99f)
                        .fillMaxWidth()
                        .weight(1f),
                    images = if (isPreview) testItemFull.pics else state.itemFull!!.pics,
                    count = state.count,
                    onIncrease = { viewModel.increase() },
                    onDecrease = { viewModel.decrease() }
                )
                ItemInfo(
                    modifier = Modifier
                        .fillMaxWidth()
                        .weight(1f)
                        .background(MaterialTheme.colorScheme.secondaryContainer)
                        .padding(16.dp),
                    itemEntity = if (isPreview) testItemEntity else state.itemFull!!.item
                )
            }
            DetailBottomBar(total = state.total, onClick = { navigateToRoute.invoke(Route.Cart) })
            DetailTopBar(backPress = { upPress.invoke() })
        } else {
            CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
        }

    }
}

@Preview(showBackground = true)
@Composable
fun DetailScreenPreview() {
    OnLineShopAppTheme {
        DetailScreen(isPreview = true, param = Route.Detail(1), upPress = {})
    }
}